﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace visu.Controllers
{
    public class ResourceController : Controller
    {
        //
        // GET: /Resource/
        #region=================================即时库存==================================================
        public ActionResult OnTimeInventory()
        {
            return View();
        }
        public ActionResult OnTimeInventoryDetail(int page, int rows, vinventoryontime vinventoryontime, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "PRODUCT_NO" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);

            Expression<Func<vinventoryontime, bool>> exp = SearchExpression.GetSearchExpression<vinventoryontime>(vinventoryontime, listSearch, SearchContains, "M_CODE", "M_SPECIFICATION", "M_TYPE");

            var list = BLLSession.Current.vinventoryontimeBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.vinventoryontimeDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region=================================汇总库存==================================================
        public ActionResult InventorySummaryInfo()
        {
            return View();
        }
        public ActionResult InventorySummaryInfoDetail(int page, int rows, inventorysummarybyproduct inventorysummarybyproduct, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "PRODUCT_NO" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);

            Expression<Func<inventorysummarybyproduct, bool>> exp = SearchExpression.GetSearchExpression<inventorysummarybyproduct>(inventorysummarybyproduct, listSearch, SearchContains, "M_CODE", "M_SPECIFICATION", "M_TYPE");

            var list = BLLSession.Current.inventorysummarybyproductBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.inventorysummarybyproductDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UpdateInventorySummary(string summaryType)
        {
            InventorySummary(summaryType);
            var tempObj = new { code = "success", data = "", message = "更新成功！" };
            return Json(tempObj, JsonRequestBehavior.AllowGet);
        }

        protected void InventorySummary(string summaryType)
        {
            SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = DBCallCommon.GetStringValue("connectionStrings");
            DBCallCommon.openConn(sqlConn);
            SqlCommand sqlCmd = new SqlCommand("PRO_InventorySummary", sqlConn);
            sqlCmd.CommandTimeout = 300;
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@summary_type", SqlDbType.Int, 4);
            sqlCmd.Parameters["@summary_type"].Value = summaryType;
            sqlCmd.ExecuteNonQuery();
            DBCallCommon.closeConn(sqlConn);
        }
        #endregion

    }
}
